package com.redwine.app.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.redwine.app.APP;
import com.redwine.app.R;
import com.redwine.app.ShippingAddressAddActivity;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.AddressListResponse;
import com.redwine.app.net.responses.BaseResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.Utils;
import com.redwine.app.view.AlertDialog;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * 收货地址适配器
 * 作者：gassion on 17/9/9 16:01
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class ShippingAddressAdapter extends BaseAdapter {

    private Context context;

    private List<AddressListResponse.DataEntity> datalist;


    public ShippingAddressAdapter(Context context) {
        this.context = context;
    }


    /**
     * 下拉加载数据
     *
     * @param datalist
     */
    public void setData(List<AddressListResponse.DataEntity> datalist) {
        this.datalist = datalist;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (null != datalist) {
            return datalist.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (null != datalist) {
            return datalist.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.activity_shipping_address_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final AddressListResponse.DataEntity dataEntity = datalist.get(position);
        viewHolder.mShippingAddressTvAddress.setText(dataEntity.getAddress());
        viewHolder.mShippingAddressTvName.setText(dataEntity.getName());
        viewHolder.mShippingAddressTvPhone.setText(dataEntity.getTel());
        viewHolder.mShippingAddressTvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShippingAddressAddActivity.class);
                intent.putExtra(ShippingAddressAddActivity.DATA_ENTITY, dataEntity);
                context.startActivity(intent);
            }
        });
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle(context.getString(R.string.hint));
                builder.setMessage(context.getString(R.string.sure_to_delete_the_address));
                builder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton(context.getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        requestDelAddress(dataEntity.getId());
                    }
                });
                AlertDialog mDialog = builder.create();
                mDialog.setCancelable(false);
                mDialog.show();
                return false;
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView mShippingAddressTvUpdate;
        public TextView mShippingAddressTvName;
        public TextView mShippingAddressTvPhone;
        public TextView mShippingAddressTvAddress;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mShippingAddressTvUpdate = (TextView) rootView.findViewById(R.id.shipping_address_tv_update);
            this.mShippingAddressTvName = (TextView) rootView.findViewById(R.id.shipping_address_tv_name);
            this.mShippingAddressTvPhone = (TextView) rootView.findViewById(R.id.shipping_address_tv_phone);
            this.mShippingAddressTvAddress = (TextView) rootView.findViewById(R.id.shipping_address_tv_address);
        }

    }


    private void requestDelAddress(String address_id) {
        String url = NetworkConstants.ADDRESS_DEL_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        params.put("address_id", address_id);
        GsonRequest<BaseResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BaseResponse.class, false, params, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response) {
                Utils.showToast(response.getMsg());
                if (response.isSuccess()) {
                    EventBus.getDefault().post(new SendMsgEvent(SendMsgEvent.REFURBISH_SHIPPING_ADDRESS));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(R.string.generic_check);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }
}
